package com.mobile.mo.minio;

import io.minio.*;
import io.minio.http.Method;

import java.io.ByteArrayInputStream;
import java.util.ArrayList;
import java.util.List;

/**
 * 千锋Java,为你而生！
 *
 * @Author: Feri
 * @Date:2024/11/15 15:43
 * @Modified:
 */
public class MinioUtil {
    //Minio服务器的地址
    private static final String MINIO_HOST="http://47.97.58.13:9000";
    //Minio key
    private static final String MINIO_KEY="qfjava2403";
    //Minio securet 秘钥
    private static final String MINIO_SEC="xing1688";
    //存储对象的桶
    public static final String MINIO_BUCKET="lazyhabit";

    //客户端对象
    public static MinioClient client;


    static {
        client=MinioClient.builder().endpoint(MINIO_HOST)
                .credentials(MINIO_KEY,MINIO_SEC).build();
    }

    /**
     * 创建 桶*/
    public static boolean createBucket(String name){
        try {
            //验证是否存在
            if (!client.bucketExists(BucketExistsArgs.builder().bucket(name).build())) {
                //不存在 再创建
                client.makeBucket(MakeBucketArgs.builder().bucket(name).build());
                return true;
            }
        }catch (Exception ex){
            ex.printStackTrace();
        }
        return false;
    }
    /**
     * 查询桶*/
    public static List<String> queryBuckets(){
        List<String> names=new ArrayList<>();
        try {
            client.listBuckets().forEach(b->{
                names.add(b.name());
            });
        }catch (Exception ex){
            ex.printStackTrace();
        }
        return names;
    }
    /**
     * 上传对象*/
    public static String upload(String objName,byte[] data){
        try {
            //上传对象 上传资源文件
           client.putObject(PutObjectArgs.
                    builder().bucket(MINIO_BUCKET).object(objName).
                    stream(new ByteArrayInputStream(data), data.length, 0).
                    build());
            //生成访问地址
            return createUrl(objName);
        }catch (Exception e){
            e.printStackTrace();
        }
        return null;
    }
    /**
     * 查询对象列表*/
    public static List<String> queryObjects(){
        List<String> names=new ArrayList<>();

        client.listObjects(ListObjectsArgs.builder().bucket(MINIO_BUCKET).build()).forEach(r -> {
                try {
                    names.add(r.get().objectName());
                }catch (Exception ex){
                    ex.printStackTrace();
                }
            });

        return names;
    }
    /**
     * 删除文件 对象*/
    public static boolean deleteObject(String objname) {
        try {
            client.removeObject(RemoveObjectArgs.builder().bucket(MINIO_BUCKET).object(objname).build());
            return true;
        }catch (Exception e){
            e.printStackTrace();
        }
        return false;
    }
    /**
     * 验证对象 是否存在*/
    public static boolean existsObject(String objname){
        try{
            return client.getObject(GetObjectArgs.builder().bucket(MINIO_BUCKET).object(objname).build())!=null;
        }catch (Exception e){

        }
        return false;
    }
    /**
     * 生成对象的访问地址*/
    public static String createUrl(String objName) throws Exception{

        return client.getPresignedObjectUrl(GetPresignedObjectUrlArgs.builder().
                bucket(MINIO_BUCKET).object(objName).method(Method.GET).build());
    }
}
